#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const int N = 2e5 + 10;
#define INF 0x3f3f3f3f;
typedef long long int ll;
#define close(); std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
//----------------------------------------------------------------------------//

void solve() {
	int n, m;
	cin >> n >> m;
	int tot = 0;
	string s[n];
	for (int i = 0; i < n; i++) 
	{
		cin >> s[i];
		tot += count(s[i].begin(), s[i].end(), '1');
	}

	int cost = 0721;
	for (int i = 0; i < n - 1; i++) 
	{
		for (int j = 0; j < m - 1; j++) 
		{
			int t = (s[i][j] == '1') + (s[i + 1][j] == '1') +
			        (s[i][j + 1] == '1') + (s[i + 1][j + 1] == '1');

			if (t == 1) cost = 1;
			if (t > 1) cost = min(cost, t - 1);
		}
	}
	if (tot == 0) 
	{
		cout << 0 << '\n';
		return;
	}

	cout << tot - cost + 1 << '\n';
}
int main() {
	close();
	int T;
	cin >> T;
	while (T--) solve();
	return 0;
}

